package com.example.chatchatbackend.service.impl.sendJob;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.send.SendMessage;
import com.example.chatchatbackend.entity.vo.response.SendMessageVO;
import com.example.chatchatbackend.mapper.SendMessageMapper;
import com.example.chatchatbackend.service.MessageService;
import com.example.chatchatbackend.service.sendJob.SendMessageService;
import com.example.chatchatbackend.service.SensitivewordsService;
import com.example.chatchatbackend.utils.TimeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@Service
public class SendMessageServiceImpl extends ServiceImpl<SendMessageMapper, SendMessage> implements SendMessageService {

    private static final LocalDateTime currentTime = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private SendMessageMapper sendMessageMapper;
    @Resource
    private MessageService messageService;
    @Resource
    private SensitivewordsService sensitivewordsService;



    @Override
    public List<SendMessageVO> getAll() {
        List<SendMessage> sendMessageList = sendMessageMapper.selectAll();
        List<SendMessageVO> sendMessageVOList = new ArrayList<>();
        for(SendMessage sendMessage : sendMessageList){
            sendMessageVOList.add(new SendMessageVO(sendMessage.getId(),sendMessage.getSendId(),sendMessage.getToId(),sendMessage.getMessage(),sendMessage.getIsSend(),sendMessage.getTime(),sendMessage.getSendTime(),sendMessage.getUpdateTime(),sendMessage.getCreatorId(),sendMessage.getCreatorName()));
        }
        return sendMessageVOList;
    }

    @Override
    public SendMessageVO insertSendMessage(SendMessageVO sendMessageVO) {
        SendMessage sendMessage = new SendMessage();

        BeanUtils.copyProperties(sendMessageVO, sendMessage);
        sendMessage.setIsSend(0);
        sendMessage.setTime(this.currentTime);
        sendMessage.setUpdateTime(this.currentTime);

        if(this.save(sendMessage))
            return sendMessageVO;
        return null;
    }

    @Override
    public List<SendMessageVO> insertSendMessageList(List<SendMessageVO> sendMessageVOList) {

        List<SendMessage> sendMessageList = new ArrayList<>();

        for (SendMessageVO sendMessageVO : sendMessageVOList) {
            sendMessageList.add(new SendMessage(null,sendMessageVO.getSendId(),sendMessageVO.getToId(),sendMessageVO.getMessage(),0,sendMessageVO.getTime(),this.currentTime,this.currentTime,sendMessageVO.getCreatorId(),sendMessageVO.getCreatorName()));
        }
        sendMessageMapper.insertList(sendMessageList);
        log.info("{}",sendMessageList);
        return sendMessageVOList;
    }


    @Override
    public IPage<SendMessage> pageThree(Integer pageNum, Integer pageSize) {
        Page<SendMessage> page = new Page<>(pageNum, pageSize);
        IPage<SendMessage> sendMessageIPage = sendMessageMapper.selectPage(page, null);
        return sendMessageIPage;
    }

    @Override
    public SendMessageVO updateSendMessage(SendMessageVO sendMessageVO) {
        SendMessage sendMessage = new SendMessage();
        BeanUtils.copyProperties(sendMessageVO, sendMessage);
        sendMessage.setUpdateTime(this.currentTime);
        sendMessageMapper.updateSendMessage(sendMessage);
        return sendMessageVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        sendMessageMapper.deleteBatchIds(ids);
    }

    @Override
    public List<SendMessageVO> findByIds(List<Integer> ids) {
        List<SendMessage> sendMessageList = sendMessageMapper.selectBatchIds(ids);
        List<SendMessageVO> sendMessageVOList = new ArrayList<>();

        for (SendMessage sendMessage : sendMessageList) {
            SendMessageVO sendMessageVO = new SendMessageVO();
            BeanUtils.copyProperties(sendMessage, sendMessageVO);
            sendMessageVOList.add(sendMessageVO);  // 添加到结果列表
        }

        return sendMessageVOList;
    }



    @Override
    public List<SendMessageVO> findTasksToSend(LocalDateTime currentTime) {


        List<SendMessage> sendStudents = sendMessageMapper.findSendMessageList(0,currentTime);

        List<SendMessageVO> sendStudentVos = sendStudents.stream()
                .map(sendStudent -> {
                    SendMessageVO sendStudentVo = new SendMessageVO();
                    BeanUtils.copyProperties(sendStudent, sendStudentVo);
                    return sendStudentVo;
                })
                .collect(Collectors.toList()); // 使用 collect(Collectors.toList())

        return sendStudentVos;
    }


    @Override
    public void updateIsSend(SendMessageVO sendMessageVO) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setIsSend(1);
        sendMessage.setId(sendMessageVO.getId()); // 设置要更新的记录 ID
        // 执行更新操作
        sendMessageMapper.updateById(sendMessage);
    }



}
